﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class RootItem : MonoBehaviour
{
    public ActionSub subType;
    public SubRootItem prefab;
    private ActionSubDataPanel panel;
    public Button clickBtn;
    public Dropdown dropDown;
    public Transform InspectorRoot;

    private GameObject ActionBaseData;
    private bool curState = false;
    private Data.Action action;

    private UIObjCacheProcess<SubRootItem> itemPool;
    public void SetData(Data.Action data, ActionSubDataPanel panel)
    {
        clickBtn.enabled = true;
        this.enabled = true;
        this.panel = panel;
        itemPool = panel.GetPool();
         action = data;
        if (ActionBaseData != null)
        {
            Destroy(ActionBaseData); 
        }
        if (subType == ActionSub.BaseData)
        {
            ActionBaseData = new GameObject("ActionBaseData");
            var comp = ActionBaseData.AddComponent<ActionBaseData>();
            comp.SetData(action);
            ActionBaseData.transform.SetParent(InspectorRoot.transform);
        }
        SetItemData();
    }
   
    //创建
    private void SetItemData()
    {
        if (curState == false)
            return;

        itemPool.RecycleAll();
        itemPool.ResetViewRoot(transform);
        if (subType == ActionSub.Interrupt)
        {
            for (int i = 0; i < action.ActionInterrupts.Count; i++)
            {
                var item = itemPool.Spawn();
                item.SetData(action.ActionInterrupts[i],action);
            }
        }
        else if (subType == ActionSub.AttackDef)
        {
            for (int i = 0; i < action.AttackDefs.Count; i++)
            {
                var item = itemPool.Spawn();
                item.SetData(action.AttackDefs[i], action);
            }
        }
        else if (subType == ActionSub.Event)
        {
            for (int i = 0; i < action.Events.Count; i++)
            {
                var item = itemPool.Spawn();
                item.SetData(action.Events[i], action);
            }
        }
        else if (subType == ActionSub.BaseData)
        {
            //TODO
            ActionEditorInterface.SelectObj(ActionBaseData);
        }
    }

    /// <summary>
    /// 打开或关闭
    /// </summary>
    public void ClickSelectItem()
    {
        curState = !curState;
        itemPool.RecycleAll();
        if (curState == true)
        {
           
            //打开自己
            SetItemData();
            //选择自己

            //关闭别人
            EventManager.TriggerEvent<ActionSub>(CommonEvent.SubPanel_CloseOther, subType);
        }
    }
    /// <summary>
    /// 添加数据按钮
    /// </summary>
    public void AddDataEvent()
    {
        itemPool.ResetViewRoot(transform);
        if (subType == ActionSub.AttackDef)
        {
            var data = new Data.AttackDef();
            action.AttackDefs.Add(data);
            var item = itemPool.Spawn();
            item.SetData(data, action);
        }
        else if (subType == ActionSub.Event)
        {
            var data = new Data.Event();
            data.EventType = (Data.EventType)(dropDown.value + 1);
            action.Events.Add(data);
            var item = itemPool.Spawn();
            item.SetData(data, action);
        }
        else if (subType == ActionSub.Interrupt)
        {
            var data = new Data.ActionInterrupt();
            action.ActionInterrupts.Add(data);
            var item = itemPool.Spawn();
            item.SetData(data, action);
        }
    }

    private void OnEnable()
    {
        if (subType == ActionSub.Event)
        {
            dropDown.options.Clear();
            var allCount = (int)Data.EventType.Count;
            Dropdown.OptionData data = null;
            for (int i = 1; i < allCount; i++)
            {
                data = new Dropdown.OptionData();
                data.text = ((Data.EventType)i).ToString();
                dropDown.options.Add(data);
            }
            dropDown.value = 0;
        }
        EventManager.AddEventListener<ActionSub>(CommonEvent.SubPanel_CloseOther, SubPanel_CloseOther);

        EventManager.AddEventListener<Data.ActionInterrupt>(CommonEvent.FrameItemLineSelectInterrupt, OnFrameItemLineSelectInterrupt);
        EventManager.AddEventListener<Data.Event>(CommonEvent.FrameItemLineSelectEvent, OnFrameItemLineSelectEvent);
        EventManager.AddEventListener<Data.AttackDef>(CommonEvent.FrameItemLineSelectAttackDef, OnFrameItemLineSelectAttackDef);
        EventManager.AddEventListener<ActionSub>(CommonEvent.ActionSubItemDelate, ActionSubItemDelate);
    }
    private void OnDisable()
    {
        EventManager.RemoveEventListener<ActionSub>(CommonEvent.SubPanel_CloseOther, SubPanel_CloseOther);

        EventManager.RemoveEventListener<Data.ActionInterrupt>(CommonEvent.FrameItemLineSelectInterrupt, OnFrameItemLineSelectInterrupt);
        EventManager.RemoveEventListener<Data.Event>(CommonEvent.FrameItemLineSelectEvent, OnFrameItemLineSelectEvent);
        EventManager.RemoveEventListener<Data.AttackDef>(CommonEvent.FrameItemLineSelectAttackDef, OnFrameItemLineSelectAttackDef);
        EventManager.RemoveEventListener<ActionSub>(CommonEvent.ActionSubItemDelate, ActionSubItemDelate);
    }
    private void ActionSubItemDelate(ActionSub type)
    {
        if (type == subType)
        {
            SetItemData();
        }
    }
    private void SubPanel_CloseOther(ActionSub subType)
    {
        if (subType != this.subType)
        {
            curState = false;
        }
    }

    private void OnFrameItemLineSelectInterrupt(Data.ActionInterrupt data)
    {
        InvokeFrameLineClick( ActionSub.Interrupt ,data);
    }

    private void InvokeFrameLineClick(ActionSub campareType, object data)
    {
        if (subType == campareType && curState == false) //自己是关闭的
        {
            ClickSelectItem();//先打开自己
            //选择cell
            var tempCell = itemPool.Find((item) => { return item.GetSubData() == data; });
            if (tempCell != null)
            {
                tempCell.toggle.isOn = true;
            }
        }
    }

    private void OnFrameItemLineSelectEvent(Data.Event data)
    {
        InvokeFrameLineClick(ActionSub.Event, data);
    }
    private void OnFrameItemLineSelectAttackDef(Data.AttackDef data)
    {
        InvokeFrameLineClick(ActionSub.AttackDef, data);
    }
}
